<?php
session_start();
require_once "../includes/config.php";
require_once "../includes/pagination.php";

if(!empty($_POST['delCategory']))
{ 	
	$categoryId=$_POST['delCategory'];	
 	$sqlDelCat="Delete from tbl_category where category_id='$categoryId'";
	$rstDelCat=mysql_query($sqlDelCat) or die(mysql_error());
	if($rstDelCat)
	 {			
		echo "<script type='text/javascript'>window.location='index.php?cat';</script>";
	 }
}
 

if(!empty($_POST['CategoryKey']))
{
		$catName=$_POST['cat_name'];		
		$catDescription=$_POST['cat_description'];		
		$today=date("Y-m-d");	
		
		if($_POST['CategoryKey']=="add") {
			
			if(!empty($catName)) {
				$sql_addcat="INSERT INTO tbl_category set category_name='$catName',category_description='$catDescription',created_by='$admin_id_sess',created_date='$today'";
				$msg="<div id='succ-msg'>Category added successfully.</div>";
			}
			
		}
		
		if($_POST['CategoryKey']=="edit") {
			
			if(!empty($catName)) {
				$sql_addcat="UPDATE tbl_category set category_name='$catName',category_description='$catDescription' where category_id='".$_GET['edit']."'";
				$msg="<div id='succ-msg'>Category updated successfully.</div>";	
			}
			else {
				$sql_addcat="";
				$msg="";
			}		
		}
		
		$rst_addcat=mysql_query($sql_addcat) or die(mysql_error());
		if($rst_addcat) {
			if(!empty($_GET['edit'])) {
				echo "<script type='text/javascript'> window.location=document.location.href; </script>";
			}else {
				$insmsg=$msg;
			}
		}
		else { $insmsg=""; }
}

if(!empty($_GET['edit'])) {
	$heading="Edit Category";
	$action="edit";
	$sqlEditCat="SELECT * FROM tbl_category where category_id='".$_GET['edit']."'";
	$rstEditCat=mysql_query($sqlEditCat);
	$rowEditCat=mysql_fetch_array($rstEditCat);
	$cname="<input type='text' name='cat_name' id='cat_name' value='".$rowEditCat["category_name"]."'";
	$cdesc="<input type='text' name='cat_description' id='cat_description' value='".$rowEditCat["category_description"]."'";
}

if(isset($_GET['add'])) {
	$heading="Add Category";
	$cname="<input type='text' name='cat_name' id='cat_name'";
	$cdesc="<input type='text' name='cat_description' id='cat_description'";
	$action="add";
}





//Get all categories
$itemPerPage=15;
$sqlShwCat="SELECT * FROM tbl_category where parent_id=0";
$rstShwCat=mysql_query(getPagingQuery($sqlShwCat,$itemPerPage));  
$numShwCat=mysql_num_rows($rstShwCat);

?>
<style type="text/css">
	div#add-category { margin:0 auto; padding:5px; width:55%; text-align:center; }
	
</style>
<table width="100%"   border="1" cellspacing="0" class="container">
  <tr>
    <td width="469" height="25" class="tdHeader">Swapen Categories</td>
  </tr>
  <tr>
    <td  height="577">
	 <form action="" method="post" name="formCategory" id="formCategory">	 
	  <input type="hidden" name="delCategory" id="delCategory">
	<br><br>
			<?php if(isset($_GET['add']) || isset($_GET['edit'])) { ?>
			<div id="add-category">				
               <input type="hidden" name="CategoryKey" id="CategoryKey" value="<?php echo $action; ?>">
           		 <table width="100%"   border="1" cellspacing="0" class="container">
				 <tr><td height="25" class="tdHeader" style="text-align:center;"><strong><?php echo $heading; ?></strong></td></tr>
				 <tr><td>	
					<table width="100%" border="0" cellspacing="0" align="center">
					<tr>
          			  <td height="31" colspan="2">
					  	<?php if($insmsg!="") { echo $insmsg; } ?> 
					  </td>
          			</tr>					
          			<tr>
						<td height="24">Category Name </td>
						<td>					 
							<?php echo $cname; ?>
						</td>
				    </tr>
					<tr>
					   <td height="24">Category Description </td>
					   <td><?php echo $cdesc; ?></td>
					 </tr>				            
					 <tr>
					   <td>&nbsp;</td>
					   <td>&nbsp;</td>
				      </tr>
					 <tr>					  
					   <td colspan="2" style="text-align:center;">
						<input name="Submit" type="button" value="<?php echo $heading; ?>" class="button" onClick="CategorySubmit()">
						<input name="cancel" type="button" value="Cancel" class="button" onClick="window.location='index.php?cat'">
					   </td>
					 </tr>
					 <tr>
					   <td>&nbsp;</td>
					   <td>&nbsp;</td>
				      </tr>
					</table>
				  </td></tr>
				  
				</table>	             		
			  </div>
			 <br><br>
			 <?php } ?>			 
			 <div id="list-category">
			 			<table width="100%"  border="0" cellspacing="0">
							 <tr>
							   <td height="25" colspan="5" style="text-align:right;" class="line">
							   		<input type="button" class="button" onClick="window.location='index.php?cat&add'" value="Add">
							   </td>
							 </tr>
							
							 <tr class="InnerHeader">
							   <td width="2%"  class="line">&nbsp;</td>
							   <td width="15%" class="line">Category Name</td>
							   <td width="15%" class="line">Description</td>
							   <td width="2%" class="line"></td>
							   <td width="2%" class="line"></td>
							 </tr>
						<?php
						 if($numShwCat>0) {
							 while($rowShwCat=mysql_fetch_array($rstShwCat))
							 {
								 $catId=$rowShwCat['category_id'];
								 $catName=$rowShwCat['category_name'];
								 $catDesc=$rowShwCat['category_description'];								 
								 echo "<tr>";
								 echo "<td class='line'>&nbsp;</td>";
								 echo "<td class='line'>$catName</td>";
								 echo "<td class='line'>$catDesc</td>";
								 echo "<td class='line'><input type='button' name='edit' value='Edit' class='button' onclick='editCatLink($catId);'></td>";
								 echo "<td height='20' class='line'><input type='button' name='delete' value='Delete' class='button'  onclick='deleteCatSubmit($catId);'></td> ";
								 echo "</tr>";
							 }
						}
						else { echo "<tr> <td colspan='5'> &nbsp; </td> </tr>"; } 
						?>
						 <tr>
							   <td colspan="5"></td>
						 </tr>
						 <tr>
							   <td height="25" colspan="5" style="text-align:right;">
							   	<input type="button" class="button" onClick="window.location='index.php?cat&add'" value="Add">
							   </td>
						 </tr>
						 <tr>
							   <td colspan="5">&nbsp;</td>
							 </tr>
						<tr>
							<td colspan="5">							
								<div align="center">
									<?php
										   $link="cat";
										   echo $disp=getPagingLink($sqlShwCat,$itemPerPage,$link);							   
									 ?>
								</div>
							</td>
						 </tr>
				 </table>
			 </div> 
		</form>	 		  
	  </td>
	</tr>
</table>	  	



